/*
 * lotura - GWT databinding
 * Copyright (C) 2008  Juanjo García Latre
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package net.inttes.lotura.binding.adapter;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import com.google.gwt.core.client.GWT;

import net.inttes.lotura.binding.value.ValueModel;
import net.inttes.lotura.binding.value.support.ValueHolder;


public class SelectionInList {

	private static final int NO_SELECTION_INDEX = -1;
	
	private List listItems;
	private ValueModel selectionHolder;
	private ValueModel selectionIndexHolder;
	private PropertyChangeListener selectionChangeHandler = new SelectionChangeHandler();
	private PropertyChangeListener selectionIndexChangeHandler = new SelectionIndexChangeHandler();
	

	public SelectionInList(List listItems, ValueModel selectionHolder) {
		this.listItems = listItems;
		this.selectionHolder = selectionHolder;
		this.selectionIndexHolder = new ValueHolder(new Integer(NO_SELECTION_INDEX));
		
		Object initialSelection = selectionHolder.getValue();
		if (initialSelection != null) {
			selectionIndexHolder.setValue( new Integer(listItems.indexOf(initialSelection)) );
		}
		
		this.selectionHolder.addValueChangeListener(selectionChangeHandler);
		this.selectionIndexHolder.addValueChangeListener(selectionIndexChangeHandler);
		
		GWT.log("[SelectionInList] initialized, selection " + this.selectionHolder +
				", index " + this.selectionIndexHolder, null);
	}
	
	
	public ValueModel getSelectionHolder() {
		return selectionHolder;
	}
	
	
	public ValueModel getSelectionIndexHolder() {
		return selectionIndexHolder;
	}
	
	
	public void release() {
		selectionHolder.removeValueChangeListener(selectionChangeHandler);
		selectionIndexHolder.removeValueChangeListener(selectionIndexChangeHandler);
	}
	
	
	private class SelectionChangeHandler implements PropertyChangeListener {
		public void propertyChange(PropertyChangeEvent evt) {
			Object newSelection = evt.getNewValue();
			int newIndex = listItems.indexOf(newSelection);
			
			GWT.log("[SelectionInList] selection changed, new index: " + newIndex, null);
			
			selectionIndexHolder.removeValueChangeListener(selectionIndexChangeHandler);
			selectionIndexHolder.setValue( new Integer(newIndex) );
			selectionIndexHolder.addValueChangeListener(selectionIndexChangeHandler);
		}
	}
	
	
	private class SelectionIndexChangeHandler implements PropertyChangeListener {
		public void propertyChange(PropertyChangeEvent evt) {
			int newIndex = ((Integer) evt.getNewValue()).intValue();
			Object newSelection = getSafeElementAt(newIndex);	
			
			GWT.log("[SelectionInList] index changed, new selection: " + newSelection, null);

			selectionHolder.removeValueChangeListener(selectionChangeHandler);
			selectionHolder.setValue(newSelection);
			selectionHolder.addValueChangeListener(selectionChangeHandler);
		}		
	}
	
	
	private Object getSafeElementAt(int index) {
		return (0 <= index && index < listItems.size()) ? 
				listItems.get(index) : null;
	}
}
